package team.wuyou.util.httpUtil;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import jdk.nashorn.internal.runtime.logging.Logger;
import team.wuyou.util.UuidUtil;
import team.wuyou.util.annotation.log.OperLog;
import team.wuyou.util.annotation.log.OperLogConst;

/**
 * @Description 阿里巴巴短信api调用
 * @Date 2024/1/10 10:49
 * @Wang
 **/
public class AlibabaSms {

    public static void main(String[] args) {
        AlibabaSms alibabaSms = new AlibabaSms();
        SendMessage("19838009862", UuidUtil.getVerificationCode(6));
    }

    //发送短信
    public static String SendMessage(String phoneNumber, String captcha) {
        String ServerAddress = "cn-hangzhou";
        String AccessKeyID = "LTAI5t73Wt3waQWni6YLxync";
        String AccessKeySecret = "VC4I6n8wMwcFFj0mYwwyp6uVPOWeLm";

        //手机号
        //String phoneNumber = "19838009862";
        //签名
        String signName = "阿里云短信测试";
        //短信模板id
        String templateCode = "SMS_154950909";

        System.out.println("验证码:" + captcha);
        //短信内容
        String templateParam = "{\"code\":\"" + captcha + "\"}";

        //加载配置文件
        DefaultProfile profile = DefaultProfile.getProfile(ServerAddress, AccessKeyID, AccessKeySecret);
        //创建客户端
        IAcsClient client = new DefaultAcsClient(profile);
        // 创建API请求并设置参数
        SendSmsRequest request = new SendSmsRequest();

        //配置参数
        //设置手机号
        request.setPhoneNumbers(phoneNumber); // 该参数值为假设值，请您根据实际情况进行填写
        //签名
        request.setSignName(signName); // 该参数值为假设值，请您根据实际情况进行填写
        //设置短信模板id
        request.setTemplateCode(templateCode);
        //设置短信模板参数
        request.setTemplateParam(templateParam);
        String json = null;
        try {
            //发送数据，获取响应
            SendSmsResponse response = client.getAcsResponse(request);
            json = JSON.toJSONString(response);
            System.out.println(json);
            // 打印您需要的返回值，此处打印的是此次请求的 RequestId
            //System.out.println(response.getRequestId());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            // 打印错误码
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }
        return json;
    }
}
